package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ljc
 * @version V1.0
 * @date 2025-03-05 20:01
 */
@Service
public class MemberReservationServiceImpl extends ServiceImpl <MemberReservationMapper, Reservation> implements MemberReservationService  {
    @Autowired
    private MemberReservationMapper memberReservationMapper;

    @Override
    public List<ReservationVo> countByTime(Long time) {
        //将时间戳转换为LocalDateTime
        LocalDateTime dateTime = LocalDateTimeUtil.of(time);
        long localTime = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(localTime);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
            List<ReservationVo> reservationVos = memberReservationMapper.countByTime(dateTime, startTime, endTime);
        return reservationVos;
    }

    @Override
    public Integer getCancelledReservationCount() {
        //获取用户id
        Long userId = UserThreadLocal.getUserId();
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        List<Reservation> cancelledReservationCount = memberReservationMapper.getCancelledReservationCount(userId, startTime, endTime);
        if (cancelledReservationCount == null){
           return 0;
        }
        return cancelledReservationCount.size() ;
    }
}
